package net.suncaper.psychological.mapper.impl;

import net.suncaper.psychological.mapper.TimeConfigMapper;
import net.suncaper.psychological.model.domain.TimeConfig;
import net.suncaper.psychological.common.utils.MyDbUtil;
import java.util.List;
import java.util.Map;

/**
 * 时间配置数据访问层实现类
 */
public class TimeConfigMapperImpl implements TimeConfigMapper {

    @Override
    public boolean save(TimeConfig timeConfig) {
        String sql = "INSERT INTO time_config (config_type, name, start_time, end_time, interval_minutes, duration, days, description, is_active, created_time, updated_time) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        
        int rows = MyDbUtil.executeUpdate(sql, 
            timeConfig.getConfigType(),
            timeConfig.getName(),
            timeConfig.getStartTime(),
            timeConfig.getEndTime(),
            timeConfig.getIntervalMinutes(),
            timeConfig.getDuration(),
            timeConfig.getDays(),
            timeConfig.getDescription(),
            timeConfig.getIsActive(),
            timeConfig.getCreatedTime(),
            timeConfig.getUpdatedTime()
        );
        
        return rows > 0;
    }

    @Override
    public boolean deleteById(Long id) {
        String sql = "DELETE FROM time_config WHERE id = ?";
        int rows = MyDbUtil.executeUpdate(sql, id);
        return rows > 0;
    }

    @Override
    public boolean update(TimeConfig timeConfig) {
        String sql = "UPDATE time_config SET config_type = ?, name = ?, start_time = ?, end_time = ?, interval_minutes = ?, duration = ?, days = ?, description = ?, is_active = ?, updated_time = ? WHERE id = ?";
        
        int rows = MyDbUtil.executeUpdate(sql,
            timeConfig.getConfigType(),
            timeConfig.getName(),
            timeConfig.getStartTime(),
            timeConfig.getEndTime(),
            timeConfig.getIntervalMinutes(),
            timeConfig.getDuration(),
            timeConfig.getDays(),
            timeConfig.getDescription(),
            timeConfig.getIsActive(),
            timeConfig.getUpdatedTime(),
            timeConfig.getId()
        );
        
        return rows > 0;
    }

    @Override
    public TimeConfig findById(Long id) {
        String sql = "SELECT id, config_type as configType, name, start_time as startTime, end_time as endTime, interval_minutes as intervalMinutes, duration, days, description, is_active as isActive, created_time as createdTime, updated_time as updatedTime FROM time_config WHERE id = ?";
        List<TimeConfig> timeConfigs = MyDbUtil.executeQuery(TimeConfig.class, sql, id);
        return timeConfigs.isEmpty() ? null : timeConfigs.get(0);
    }

    @Override
    public List<TimeConfig> findAll() {
        String sql = "SELECT id, config_type as configType, name, start_time as startTime, end_time as endTime, interval_minutes as intervalMinutes, duration, days, description, is_active as isActive, created_time as createdTime, updated_time as updatedTime FROM time_config ORDER BY created_time DESC";
        return MyDbUtil.executeQuery(TimeConfig.class, sql);
    }

    @Override
    public List<TimeConfig> findByConfigType(String configType) {
        String sql = "SELECT id, config_type as configType, name, start_time as startTime, end_time as endTime, interval_minutes as intervalMinutes, duration, days, description, is_active as isActive, created_time as createdTime, updated_time as updatedTime FROM time_config WHERE config_type = ? ORDER BY created_time DESC";
        return MyDbUtil.executeQuery(TimeConfig.class, sql, configType);
    }

    @Override
    public List<TimeConfig> findByIsActive(Boolean isActive) {
        String sql = "SELECT id, config_type as configType, name, start_time as startTime, end_time as endTime, interval_minutes as intervalMinutes, duration, days, description, is_active as isActive, created_time as createdTime, updated_time as updatedTime FROM time_config WHERE is_active = ? ORDER BY created_time DESC";
        return MyDbUtil.executeQuery(TimeConfig.class, sql, isActive);
    }

    @Override
    public List<TimeConfig> findByConfigTypeAndIsActive(String configType, Boolean isActive) {
        String sql = "SELECT id, config_type as configType, name, start_time as startTime, end_time as endTime, interval_minutes as intervalMinutes, duration, days, description, is_active as isActive, created_time as createdTime, updated_time as updatedTime FROM time_config WHERE config_type = ? AND is_active = ? ORDER BY created_time DESC";
        return MyDbUtil.executeQuery(TimeConfig.class, sql, configType, isActive);
    }

    @Override
    public boolean updateIsActiveByIds(List<Long> ids, Boolean isActive) {
        if (ids == null || ids.isEmpty()) {
            return false;
        }
        
        StringBuilder sql = new StringBuilder("UPDATE time_config SET is_active = ? WHERE id IN (");
        for (int i = 0; i < ids.size(); i++) {
            if (i > 0) {
                sql.append(", ");
            }
            sql.append("?");
        }
        sql.append(")");
        
        Object[] params = new Object[ids.size() + 1];
        params[0] = isActive;
        for (int i = 0; i < ids.size(); i++) {
            params[i + 1] = ids.get(i);
        }
        
        int rows = MyDbUtil.executeUpdate(sql.toString(), params);
        return rows > 0;
    }

    @Override
    public List<Map<String, Object>> findMapsByConfigType(String configType) {
        String sql = "SELECT * FROM time_config WHERE config_type = ? ORDER BY created_time DESC";
        return MyDbUtil.executeQueryMap(sql, configType);
    }
} 